/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(nullptr == head)
            return nullptr;
        // 头节点删除满足条件
        while(head != nullptr && head->val == val){
            ListNode* del = head;
            head = head->next;
            delete del;
        }
        // 删除头节之后满足的节点
        ListNode* cur = head; 
        while(cur != nullptr && cur->next != nullptr){
            ListNode* del = cur->next;
            if(del->val == val){ // 满足删除条件
                cur->next = del->next;
                delete del;
            }  
            else{
                cur = del;
            }
        }
        return head;
    }
};